// common js functionality

// global shortcut to redirect url
function _redirect(uri)
{
	document.location.href = uri;
}

// submits the selected obj form to mymovies.add
function add_collection(obj)
{
	obj.elements[0].value	= 'mymovies';
	obj.elements[1].value	= 'add';

	obj.submit();
}

// submits the selected obj form to wishlist.add
function add_wishlist(obj)
{
	obj.elements[0].value	= 'wishlist';
	obj.elements[1].value	= 'add';
	obj.submit();
}

// submits the selected obj form to mymovies.borrow
function mark_borrowed(obj)
{
	obj.elements[0].value	= 'mymovies';
	obj.elements[1].value	= 'borrow';
	obj.submit();
}

// launch the help window
function launch_help()
{
	window.open('index.php?what=help', 'help', 'status=0, toolbar=0, width=300, height=600, resizable=1, scrollbars=1');
}

// adds the movie_id to custom list
function movie_list_add(movie_id)
{
	var popup = window.open('index.php?what=list&do=add&id='+ movie_id, 'movie_list', 'width=250, height=400');
	if(!popup.opener) {
		popup.opener = self;
	}
}

// verifies if the user wants to remove the selected movies from their collection & submits the form
function remove_collection(form_id)
{
	if(select_all_current > 0) {
		if(confirm('Are you sure you want to remove the selected movies from your collection?')) {
			submit_form(form_id);
		}
	} else {
		alert('No movies selected');
	}
}

// submit the selected form
function submit_form(form_id)
{
	var frm = gId(form_id);
	if(frm) {
		frm.submit();
	}
}

// select all functionality
var select_all_form		= '';
var select_all_count	= 0;
var select_all_current	= 0;

function init_select_all(frm_id)
{
	var frm	= gId(frm_id);
	select_all_form	= frm_id;

	gId('select_all').onclick = function()
	{
		var frm	= gId(select_all_form);
		var cb	= frm.getElementsByTagName('INPUT');

		var stat	= this.checked;

		if(stat) {
			select_all_current	= select_all_count;
		} else {
			select_all_current	= 0;
		}

		for(var i = 0; i < cb.length; i++) {
			c	= cb.item(i);
			if(c.type == 'checkbox' && cb.id != 'select_all') {
				c.checked	= stat;
			}
		}
	}

	var cb	= frm.getElementsByTagName('INPUT');

	for(var i = 0; i < cb.length; i++) {
		c = cb.item(i);
		if(c.type == 'checkbox' && c.id != 'select_all') {
			select_all_count++;
			cb.item(i).onclick = function() {
				if(this.checked) {
					select_all_current++;
				} else {
					select_all_current--;
				}

				if(select_all_current == select_all_count) {
					gId('select_all').checked = true;
				} else {
					gId('select_all').checked = false;
				}
			}
		}
	}
}

// add in_array (from php) functionality to all arrays!
Array.prototype.inArray = function(value, pos)
{
	if(this.constructor.toString().indexOf("Array") == -1) {
		return false;
	}

	for(z = 0; z < this.length; z++) {
		if(this[z].toString() === value.toString()) {
			if(pos == true) {
				return z;
			}
			return true;
		}
	}
	return false;
}

function replace(s, t, u) {
	i = s.indexOf(t);
	r = "";
	if (i == -1) return s;
	r += s.substring(0,i) + u;
	if ( i + t.length < s.length) {
		r += replace(s.substring(i + t.length, s.length), t, u);
	}
	return r;
}

// Dynamically add a new input field to the specified form
function add_field(form_id, field_type, field_name, field_value)
{
	if (document.getElementById) {
		var input = document.createElement('INPUT');
		// what follows should work with NN6 but doesn't in M14
		if (document.all) {
			input.type = field_type;
			input.name = field_name;
			input.value = field_value;
		} else if (document.getElementById) { // so here is the NN6 workaround
			input.setAttribute('type', field_type);
			input.setAttribute('name', field_name);
			input.setAttribute('value', field_value);
		}
		gId(form_id).appendChild(input);
	}
}

var menu_sel = '';

// start the welcome menu
function start_menu()
{
	wtab = gId('wtabs');

	// loop through menu links
	wtabs_links = wtab.getElementsByTagName('A');

	for(i = 0; i < wtabs_links.length; i++) {
		menu_id = wtabs_links[i].id.split('_');
		menu_id.shift();
		menu_id = menu_id.join('_');

		wtabs_links[i].href = '#'+menu_id;

		wtabs_links[i].onclick = function()
		{
			menu_id = this.id.split('_');
			menu_id.shift();
			menu_id = menu_id.join('_');
			call_menu(menu_id);
		}

		gId(menu_id).style.display = 'none';

		if(i == 0) {
			menu_sel = menu_id;
		}
	}

	if(self.location.hash != '') {
		lnk	= self.location.hash.substr(1, self.location.hash.length);
		if(gId('t_' + lnk)) {
			menu_sel = lnk;
		}
	}

	call_menu(menu_sel);

}

function call_menu(obj)
{
	if(gId(obj)) {
		// unselect the old tab
		gId(menu_sel).style.display = 'none';
		gId('t_'+menu_sel).className = '';

		// select the new tab
		gId(obj).style.display = 'block';
		gId('t_'+obj).className = 'selected';

		menu_sel = obj;
	}
}

// shortcut for getElementById
function gId(id)
{
	return document.getElementById(id);
}

// shortcut for getElementsByTagName
function gTagName(name)
{
	return document.getElementsByTagName(name);
}